package com.wshl.aijiuunion.utils;

import android.os.Handler;
import android.os.Message;

/**
 * Created by Administrator on 2019/2/20.
 */

public class CountDownHandler extends Handler {
    int totalSeconds;
    int intervalSeconds;
    int tempSeconds;
    protected static final int COUNT_DOWN_START = 0;
    protected static final int COUNT_DOWN_STOP = 1;

    public CountDownHandler(int totalSeconds, int intervalSeconds) {
        this.totalSeconds = totalSeconds;
        this.intervalSeconds = intervalSeconds;
        this.tempSeconds = totalSeconds;
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case COUNT_DOWN_START:
                tempSeconds--;
                if (tempSeconds > 0) {
                    Message message = obtainMessage(COUNT_DOWN_START);
                    sendMessageDelayed(message, intervalSeconds);
                    onTick(tempSeconds);
                } else {
                    onFinished();
                    tempSeconds = totalSeconds;
                }
                break;
            case COUNT_DOWN_STOP:
                tempSeconds = totalSeconds;
                break;
        }
    }

    protected void onTick(int surplus) {
    }

    ;

    protected void onFinished() {
    }

    ;

}
